<?php

# Initialize Giocondo.
# This file should be included after any action that should be repeated on 
# every client request (e.g. setting up resources) and before any action that 
# should not be repeated on every request (e.g. create widget instances).
#
# In a future GTK implementation of Giocondo this file will do almost nothing
# because no state need to be saved and restored and there are no AJAX requests 
# to handle.

# TODO: enable namespace support when PHP 5.3.0 becomes stable.
#use Giocondo as G;

function __autoload($class_name) {
	# TODO: probably should be modified when enabling namespace. See here:
	# http://blog.felho.hu/whats-new-in-php-53-part-1-namespaces.html
	require_once "$class_name.php";
}

# If register_globals is on, saving global variables to session
# would allow session variable poisoning (see main_loop function).
# TODO: Removed as of PHP 6.0.0. This test should be removed in the future.
if (ini_get('register_globals')) {
	# TODO: when enabling namespace should be G::Error and so on...
	Error::trigger('For security reasons register_globals must be Off', E_USER_ERROR);
}

# TODO: Removed as of PHP 6.0.0. This test should be removed in the future.
if (get_magic_quotes_gpc()) {
	Error::trigger('magic_quotes_gpc ON is not supported', E_USER_ERROR);
}

# TODO: Removed as of PHP 6.0.0. This test should be removed in the future.
if (ini_get('register_long_arrays')) {
	Error::trigger('register_long_arrays ON is not supported', E_USER_ERROR);
}

# All global variables used by Giocondo are prefixed with Giocondo.
# This prefix is reserved and should never be used by client code
# to avoid collisions.
# TODO: consider ups and downs of using the registry pattern
# or one global array

# Contains client-side JavaScript code to set per-widgets objects properties
# and configuration option.
# When the widget is displayed (but also when it is displayed in the hide box)
# only properties that cannot be represented as HTML attributes will be setted 
# using JavaScript.
# Else if the widget has only been reconfigured in an Ajax request we are 
# handling this JavaScript code will be the Ajax response the client should
# execute.
# Position 0 and fake event onError is used to transmit errors (see Error.php).
$Giocondo_js = array();

# List of the new widgets (ids are given as keys, with corresponding widgets as 
# values).  They are all the widgets created in this request.  When a widget is 
# displayed it is removed from this list.  Thus when all windows have been 
# displayed only widgets to add to the hide box will remain.
$Giocondo_new_wid = array();

# New windows created in this request.
$Giocondo_new_win = array();

# Variables that shouldn't be registered in session.
$Giocondo_no_save = array(
	'GLOBALS', '_POST', '_GET', '_COOKIE', '_FILES', '_ENV', '_REQUEST', 
	'_SERVER', '_SESSION', 'Giocondo_callback', 'Giocondo_command', 'Giocondo_e', 
	'Giocondo_e_name', 'Giocondo_id', 'Giocondo_js', 'Giocondo_no_save', 
	'Giocondo_new_wid', 'Giocondo_new_win', 'Giocondo_win_js'
);

# This is useful because trying to serialize objects containing resources may 
# lead to problems. E.g. with PDO objects on exit I get "Fatal error: Exception 
# thrown without a stack frame in Unknown on line" and session is not updated.
function no_save() {
	global $Giocondo_no_save;

	foreach (func_get_args() as $arg) {
		$Giocondo_no_save[] = $arg;
	}
}

# Used for generator attribute of meta element in HTML templates.
# TODO: use const WHOAMI inside Giocondo namespace when enabling namespace.
define('WHOAMI', 'Giocondo ToolKit (vers 27 August 2008), see giocondo.cc-ict-sud.it');

# This is useful if the user wants to execute code conditionally
# when deploying as a web rather than standalone application.
# E.g. while handling an onClicked event on a LinkButton you
# will want to exec firefox only if we are not on the web already. 
# TODO: GTK bindings should define this as FALSE
define('WEB', TRUE);

# Initialize the session that is resume state.
# This should be done before anything is outputted to the browser.
#
# Here you use REQUEST_URI, not PHP_SELF or SCRIPT_NAME to allow to embed
# using the iframe tag the same Giocondo application in one page more than 
# one time by appending different query string. See the USERS document for 
# examples.
# TODO: are errors page occurring before calling this function
# cached by some browsers? If this is the case this function
# should be called at the start of this file.
session_cache_limiter('nocache');
# Force cookie transmission under HTTPS only if the application is 
# also server under an HTTPS connection ($_SERVER['HTTPS'] is defined).
# We could also use 3 ini_set instead of session_set_cookie_params 
# but it would be longer.
# TODO: Confirm that session.cookie_path can include a query 
# string (see cookie specification).
session_set_cookie_params(0, $_SERVER['REQUEST_URI'],
	'', isset($_SERVER['HTTPS']), TRUE);
session_name('GIOCONDOSESSID');
session_start();

# Update session.
# Based on an example about emulating register globals
# found in PHP manual, chapter "Miscellaneous Questions"
# and the example of using session_destroy in the "Function Reference".
function update_state() {
	global $Giocondo_no_save;

	# Unset all of the session variables.
	$_SESSION = array();
	# Save all global variables to the user session.
	foreach ($GLOBALS as $name => $value) {
		# Avoid serializing global resource variables also.
		if (!is_resource($name) && !in_array($name, $Giocondo_no_save)) {
			$_SESSION[$name] = $value;
		}
	}
	# TODO: make sure there are not unwanted variables in session after
	# an ajax response. If not add them to the Giocondo_no_save array.

	# Exit program normally.
	exit;
}

# TODO: move where appropriate.
Error::set_handler();

if (isset($_POST['id'])) {
	# There is an AJAX request: process it and exit.
	require 'Giocondo/php/Event.php';
} else {
	# Display application.
	require 'Giocondo/php/Display.php';
}

# End of file
# vim: set ts=2 sw=2 noet:
?>
